﻿/**

 * Copyright (c) 2015-2017, FastDev 刘强 (fastdev@163.com).

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *      http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

using Mono.Cecil;
using Mono.Cecil.Cil;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OF.ILModifier.Entity;
using OF.ILModifier.Common;
using OF.ILModifier.Extension;
namespace OF.ILModifier.Common
{
    public class AssemblyHelper
    {
        private AssemblyDefinition asm;
        public string ParamContainerFullTypeName = null;
        public MethodReference ParamContainerConstructor = null;
        public MethodReference ParamContainerAddMethod = null;
        public static string ParamContainerTypeName = typeof(ParamContainer).FullName;

        public AssemblyHelper(AssemblyDefinition asm)
        {
            this.asm = asm;
            ParamContainerFullTypeName = this.ImportReference(typeof(ParamContainer)).GetFullName();
            ParamContainerConstructor = this.ImportReference(typeof(ParamContainer).GetConstructor(new Type[0]));
            ParamContainerAddMethod = this.ImportReference(typeof(ParamContainer).GetMethod("AddParam", BindingFlags.Public | BindingFlags.Instance));
        }

        public TypeReference ImportReference(Type type)
        {
            if (type == null)
            {
                return null;
            }
            return asm.MainModule.ImportReference(type, null);
        }

        public MethodReference ImportReference(MethodReference method)
        {
            if (method == null)
            {
                return null;
            }
            return asm.MainModule.ImportReference(method);
        }

        public MemberReference ImportReference(MemberReference member, IGenericParameterProvider provider)
        {
            if (member == null)
            {
                return null;
            }

            if (member is TypeReference)
            {
                return asm.MainModule.ImportReference((TypeReference)member, provider);
            }
            else if (member is FieldReference)
            {
                return asm.MainModule.ImportReference((FieldReference)member, provider);
            }
            else if (member is MethodReference)
            {
                return asm.MainModule.ImportReference((MethodReference)member, provider);
            }
            else
            {
                return member;
            }
        }

        public MethodReference ImportReference(MethodBase method)
        {
            if (method == null)
            {
                return null;
            }
            return asm.MainModule.ImportReference(method, null);
        }


        public bool IsEquals(TypeReference type, Type rawType)
        {
            return type.GetFullName().Equals(ImportReference(rawType).GetFullName());
        }

        public bool IsEquals(MethodReference method, MethodBase rawMethod)
        {
            return method.GetFullName().Equals(ImportReference(rawMethod).GetFullName());
        }
    }
}
